<?php
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_option($socket,SOL_SOCKET,SO_REUSEADDR,true);
socket_bind($socket,"0.0.0.0",9999);
socket_listen($socket);
$read_sockets[] =$socket;
$write_socks = NULL;
$except_socks = NULL;

while(true){
    $tpm_socks = $read_sockets;
    $count = socket_select($tpm_socks,$write_socks,$except_socks,NULL);
    foreach($tpm_socks as $sock){
        if($socket == $sock){
            $connSock = socket_accept($socket);
            socket_getpeername($connSock,$ip,$port);
            echo 'ip:'.$ip.'port:'.$port.'connected...'.PHP_EOL;
            $read_sockets[] = $connSock;
        }else{
            $str = "this is a Http Request";//file_get_contents('index.html');
            $len = strlen($str);
            $response = "HTTP/1.1 200 OK\r\n";
            $response .= "Content-Type: text/html\r\n";
            $response .= "Content-Length: $len\r\n\r\n";
            $response .= "$str";
            @socket_write($sock,$response,strlen($response));
        }
    }
}
